package com.ssm.controllor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.mapper.User;
import com.ssm.service.UserService;

@Controller  
@RequestMapping("/user")  
public class UserController {  
	
	final Logger logger = LoggerFactory.getLogger(UserController.class);
	
    @Autowired  
    private UserService userService;  	
    
    //访问路径 http://localhost:8082/SSMProject/user/logger
    @RequestMapping("/logger")
    public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

         resp.getWriter().println("test logger");
         
         logger.info("这是一条日志信息 - {}", "jiafuwei");
    }
    //访问路径 http://localhost:8082/SSMProject/user/save?username=aaa&password=bbb
    @RequestMapping(value = "save")  
    public String save(HttpServletRequest request) {  
        String username = request.getParameter("username");  
        String password = request.getParameter("password");  
        System.out.println(username + " " + password);  
        System.out.println("controller save...");
        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        request.setAttribute("user", user1);
        
        User user2 = userService.selectById(1);
        System.out.println("密码："+user2.getPassword());
        return "success";  
    }  
    
    //访问路径 http://localhost:8082/SSMProject/user/view/jiafuwei
    /**Spring MVC RESTful JSON**/
    @RequestMapping(value = "/view/{username}", method = RequestMethod.GET)
    @ResponseBody
    public User view(@PathVariable String username){
        User user = new User();
        user.setPassword("123");
        user.setUsername(username);
        System.out.println("view username:"+username);

        return user;
    }

    //访问路径  http://localhost:8082/SSMProject/user/query?username=jiahaha
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public User query(@RequestParam(value="username", required=true) String username){
        User user = new User();
        user.setPassword("123");
        user.setUsername(username);
        System.out.println("view username:"+username);

        return user;
    }

  
    //访问路径  http://localhost:8082/SSMProject/user/list
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody
    List<User> listPerson(@RequestParam(value = "username", required = false, defaultValue = "") String name) {

        User user = new User();
        user.setPassword("123");
        user.setUsername("haha");
        
        User user2 = new User();
        user2.setPassword("frekkk");
        user2.setUsername("zheshi");
        
        List<User> listUser = new ArrayList<User>();
        listUser.add(user);
        listUser.add(user2);

        return listUser;
    }
    

}
